﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace Jian.Vubackup.MySql.Tool
{
    public static class JTool
    {
        /// <summary>
        /// 获得当前绝对路径--从跟节点找即 ~/
        /// </summary>
        /// <param name="strPath">指定的路径</param>
        /// <returns>绝对路径</returns>
        public static string GetRootMapPath(string strPath)
        {
            string rootDir = AppContext.BaseDirectory;

            string dir = string.Empty;
            if (!string.IsNullOrEmpty(strPath))
            {
                strPath = strPath.Replace("/", "\\");
                if (strPath.StartsWith("\\"))
                {
                    strPath = strPath.Substring(strPath.IndexOf('\\', 1)).TrimStart('\\');
                }
                dir = Path.Combine(rootDir, strPath);
            }
            else
            {
                dir = rootDir;
            }

            return dir;
        }

        /// <summary>
        /// 转为long
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static long GetNoNullLong(object obj)
        {
            if (obj != null)
            {
                return ObjectToLong(obj, 0);
            }
            return 0;
        }

        /// <summary>
        /// 将对象转换为long类型
        /// </summary>
        /// <param name="obj">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的long类型结果</returns>
        public static long ObjectToLong(object obj, long defValue)
        {
            if (obj != null)
            {
                return StrToLong(GetNullableString(obj), defValue);
            }
            return defValue;
        }

        public static string GetNullableString(object obj)
        {
            if (obj == null || obj.ToString() == "")
            {
                return null;
            }
            return obj.ToString();
        }

        /// <summary>
        /// 将对象转换为Long类型
        /// </summary>
        /// <param name="str">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的long类型结果</returns>
        public static long StrToLong(string str, long defValue)
        {
            if (string.IsNullOrEmpty(str) || str.Trim().Length >= 20 || !Regex.IsMatch(str.Trim(), "^([-]|[0-9])[0-9]*(\\.\\w*)?$"))
            {
                return defValue;
            }
            long result;
            if (long.TryParse(str, out result))
            {
                return result;
            }
            return defValue;
        }

        /// <summary>
        /// 时间转时间戳(JavaScript)毫秒数
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static long DatetimeToTimeStampJavaScript(DateTime time)
        {
            DateTime startTime = TimeZoneInfo.ConvertTime(new System.DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc), TimeZoneInfo.Local);
            long timeStamp = (long)(time - startTime).TotalMilliseconds; // 相差秒数
            return timeStamp;
        }
    }
}
